package com.qianchang.microfinance.view.membercentre;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.utils.ViewUtils;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.MemberLeverListBean;
import com.qianchang.microfinance.bean.MineUserInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.hsmywallet.HSRecharCentreActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by SSC on 2017/12/28.
 *
 *  会员中心
 */

public class MemberCentreActivity extends BaseActivity{

    @BindView(R.id.lv_id)
    ListView lvember;
    @BindView(R.id.member_head_img)
    ImageView headImg;
    @BindView(R.id.tv_phone_id)
    DTextView phoneText;
    @BindView(R.id.tv_memberLevel_id)
    DTextView tvMemberLevel;
    @BindView(R.id.tv_goldbalance_id)
    TextView goldBalance;
    @BindView(R.id.grand_total_id)
    TextView tvGrandTotal;

    private MemberCentreAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_membercentre_layout);
        ButterKnife.bind(this);
        initView();
        initData();

    }


    @Override
    protected void initView() {
        super.initView();
        String title = getString(R.string.member_centre);
        setTitle(title);

        adapter = new MemberCentreAdapter(MemberCentreActivity.this);
        lvember.setAdapter(adapter);

    }

    @Override
    protected void initData() {
        super.initData();
        getMemberLevleList();
        getUserInfo();
    }

    /**
     * 2018.1.19鸿胜科技新增加
     */
    public void getMemberLevleList() {
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.ApiURL.MEMBER_LEVLE_LIST, params, new RequestCallBack<List<MemberLeverListBean> >() {
            @Override
            public void onSuccess(JsonResult<List<MemberLeverListBean>> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    List<MemberLeverListBean> listMembers = response.getObject();
                    refreshView(listMembers);

                } else {
                    if (!"300001".equals(response.getCode())) {
                        if (!"300084".equals(response.getCode())) {
                            ToastUtil.getInstant().show(getApplicationContext(), response.getDescription());
                        }
                    } else {
                        DMApplication.getInstance().setUserLoginInfo(null);
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(getApplicationContext(), msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    private void refreshView(List<MemberLeverListBean> listMembers){
        adapter.setMemberLeveData(listMembers);
        if(listMembers != null && listMembers.size() > 0){
            MemberLeverListBean memberLeverListBean = listMembers.get(0);
            String total = memberLeverListBean.getTotalAmount();
            tvGrandTotal.setText(total);
        }else{
            tvGrandTotal.setText("");
        }
    }


    private void refreshMineView(MineUserInfo userInfo){
        if(userInfo != null){
            String imgurl = userInfo.getImageUrl();
            Glide.with(getApplicationContext()).load(imgurl).error(R.drawable.btn_head).into(headImg);
            phoneText.setTextContent(ViewUtils.formatPhone(mineUserInfo.getPhone()));
            tvMemberLevel.setTextContent("V" + userInfo.getMemberLevel());
            goldBalance.setText(userInfo.getBalanceAmount());
        }else{
            Glide.with(getApplicationContext()).load("").error(R.drawable.btn_head).into(headImg);
            phoneText.setTextContent("");
            tvMemberLevel.setTextContent("");
            goldBalance.setText("");
        }
    }


    /**
     * 2018.1.18鸿胜科技新增加
     */

    private MineUserInfo mineUserInfo;
    public void getUserInfo() {
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.ApiURL.FIND_USER_INFO, params, new RequestCallBack<MineUserInfo>() {
            @Override
            public void onSuccess(JsonResult<MineUserInfo> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    mineUserInfo = response.getObject();
                    refreshMineView(mineUserInfo);

                } else {
                    refreshMineView(null);
                    if (!"300001".equals(response.getCode())) {
                        if (!"300084".equals(response.getCode())) {
                            ToastUtil.getInstant().show(getApplicationContext(), response.getDescription());
                        }
                    } else {
                        DMApplication.getInstance().setUserLoginInfo(null);
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(getApplicationContext(), msg);
                refreshMineView(null);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }




    @OnClick({R.id.btn_nowbuy_id})
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.btn_nowbuy_id:        //
                startActivity(HSRecharCentreActivity.class);
                break;
            default:
                break;
        }
    }




}
